/* 
Creates the figures and tables for the analysis of the CUT reform in the establishment sample
	// input: estlevel_dataset, estlevel_dataset_full, microregions_municipality_concordance, shdaysleave, shdaysleavefem
	// output: 
		- firmenv; y_ln_tot_did; y_sh_probw1_fem_did; y_ln_wbill_tot_did; y_lnwage_neww_fem_did; y_lnwage_neww_mal_did; 
	    - TableA14.xlsx, sheet: tableA14
		- Table7.xlsx (sheets: employment_table, wages_table, workdays_table)
*/ 

cap log close
cap log using "$logs/establevel_figures_log", replace


*********************************
** CHANGES IN FIRM ENVIRONMENT **
*********************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (main spells)
	use "$files/estlevel_dataset.dta", clear

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	* Outcome variables
	gen sh_mgmt_fem_mgmt = mgmt_fem/mgmt
	label var sh_mgmt_fem_mgmt 		"Share of female managers (among management)"
	gen sh_mlext_mleav = mlext_mleav/mleav
	label var sh_mlext_mleav			"Share on extended maternity leave (among workers on maternity leave)"
	gen sh_mlprot_mleav = mlprot_mleav/mleav
	label var sh_mlprot_mleav			"Share returning after maternity leave (among workers on maternity leave)"
	gen sh_ileav_tot = ileav_tot/tot
	label var sh_ileav_tot 		"Share on workers on injury leave"
	
	//number of maternity leaves (at baseline, control, where amenities change)
	gen sharewomen_bl = tot_fem/tot
	sum mleav if treat==0 & year==2014 & sharewomen_bl<0.6
	
	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}

	//table with baseline mean
	local i = 0
	foreach var of varlist sh_mgmt_fem_mgmt sh_mlext_mleav sh_mlprot_mleav sh_ileav_tot {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a`i'
	}			
	#d ;
	estout a1 a2 a3 a4 using "$tables/firmenv_table.txt", 
	style(tab) mlabels(none) label collabels(none)
	cells(b(star fmt(%9.3f)) se(par)) 
	stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
	drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
	#d cr
	
	//figure wrt baseline
	local i = 0
	foreach var of varlist sh_mgmt_fem_mgmt sh_mlext_mleav sh_mlprot_mleav sh_ileav_tot {
		local i = `i'+1 
		su `var' if treat == 1 & year==2014
		gen xxx = `var'/`r(mean)'
		reghdfe xxx treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		drop xxx
		estimates store a`i'
	}	
	coefplot (a1, aseq(`""Share women" "among managers""') \ ///
			  a2, aseq(`""Take extended" "maternity leave""') \ ///
			  a3, aseq(`""Return from" "maternity leave""') \ ///
			  a4, aseq(`""Share taking" "injury leave""') ///
			  lcol(sourapple1*.8)bcol(sourapple1*.8)mcol(sourapple1*.8) barwidth(0.75)), ///
			  noci recast(bar) graphregion(color(white)) ///
			  vertical keep(treatpost) ///
			  legend(off) yline(0, lc(sourapple4) lp(dash)) ///
			  ytitle("Treatment effect (relative to baseline)", size(medlarge) height(5)) ///
			  legend(off) xlabel(, nolabel) ///
			  ylabel(-.05 "-5%" 0 "0%" 0.05 "5%" 0.10 "10%" 0.15 "15%", labsize(medium)) ///
			  eqlabel(, nolabel) ///
			  text(0.02 1 "**", size(medium) placement(n)) /// 
			  text(0.14 3 "***", size(medium) placement(n)) 
	graph export "$figures/firmenv.tif", replace

	

********************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (PANEL B) **
** DID FIGS: log employment and share of women among prob workers **
********************************************************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (all spells)
	use "$files/estlevel_dataset_full.dta", clear

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	* Outcomes
	gen y_ln_tot = log(tot)
	label var y_ln_tot 				"Log employment"
	gen y_sh_tot_fem_tot = tot_fem/tot
	label var y_sh_tot_fem_tot 		"Share female (all workers)"
	gen y_sh_probw1_fem = probw1_fem/probw1
	label var y_sh_probw1_fem 		"Share female (probationary workers)"
	gen y_ln_neww = log(neww)
	label var y_ln_neww 			"Log new workers"	
	gen y_sh_neww_fem = neww_fem/neww
	label var y_sh_neww_fem 		"Share female (new workers)"		
	gen y_sh_sepw_fem = sepw_fem/sepw
	label var y_sh_sepw_fem 		"Share female (separated workers)"
	gen exit2017 = exit if year==2017
	label var exit 					"Establishment in RAIS"
	
	//separation rate for women (at baseline, control, where amenities change)
	gen sharewomen_bl = tot_fem/tot
	gen sep_rate_fem = sepw_fem/tot_fem
	sum sep_rate_fem if treat==0 & year==2014 & sharewomen_bl<0.6

	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}
	
	mdesc y_*

	* Figure for log employment
	reghdfe y_ln_tot treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
	local coeff = _b[treatpost]
	local coeff : di %04.3f `coeff'
	local serr = _se[treatpost]
	local serr : di %04.3f `serr'

	reghdfe y_ln_tot interj*, absorb(fakeid_estab *FE) cluster(clustergrp)
	estimates store y_ln_tot

	#d ;
	coefplot (y_ln_tot ,connect(l) lpatt(solid)lcol(sourapple1)
	mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
	text(0.05 4 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
	omitted keep( interj* ) 
	yline(0, lc(sourapple4) lp(solid)) 
	xline(`xl', lc(sourapple4) lp(dash)) 
	vertical ytitle("`: var label y_ln_tot'", height(5) size(medlarge)) 
	ylabel(-0.06(0.03)0.06, labsize(medium) gmax gmin) 
	xlabel(, labsize(medium))
	plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
	#d cr 
	 
	graph export "$figures/y_ln_tot_did.tif", replace

	* Figure for share female among probationary workers
	reghdfe y_sh_probw1_fem treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
	local coeff = _b[treatpost]
	local coeff : di %04.3f `coeff'
	local serr = _se[treatpost]
	local serr : di %04.3f `serr'

	reghdfe y_sh_probw1_fem interj*, absorb(fakeid_estab *FE) cluster(clustergrp)
	estimates store y_sh_probw1_fem

	#d ;
	coefplot (y_sh_probw1_fem ,connect(l) lpatt(solid)lcol(sourapple1)
	mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
	text(0.025 4 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
	omitted keep( interj* ) 
	yline(0, lc(sourapple4) lp(solid)) 
	xline(`xl', lc(sourapple4) lp(dash)) 
	vertical ytitle("`: var label y_sh_probw1_fem'", height(5) size(medlarge)) 
	ylabel(-0.02(0.01)0.03, labsize(medium) gmax gmin) 
	xlabel(, labsize(medium))
	plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
	#d cr 
	 
	graph export "$figures/y_sh_probw1_fem_did.tif", replace
		
	//table with baseline mean
	local i = 0
	foreach var of varlist y_ln_tot y_sh_tot_fem_tot y_sh_probw1_fem y_ln_neww y_sh_neww_fem y_sh_sepw_fem {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a`i'
	}	
	preserve
		local i = `i'+1 
		drop treatpost
		rename treat treatpost
		qui reghdfe exit2017 treat, absorb(`geoglevel' ind2d_num) cluster(clustergrp)
		su exit2017 if treat == 1 & year==2017
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a`i'
	restore
	#d ;
	estout a1 a2 a3 a4 a5 a6 a7 using "$tables/employment_table.txt", 
	style(tab) mlabels(none) label collabels(none)
	cells(b(star fmt(%9.3f)) se(par)) 
	stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
	drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
	#d cr
	
	import delimited "$tables/employment_table.txt", clear
	rename v1 coeffs
	rename v2 y_ln_tot
	rename v3 y_sh_tot_fem_tot
	rename v4 y_sh_probw1_fem
	rename v5 y_ln_neww
	rename v6 y_sh_neww_fem
	rename v7 y_sh_sepw_fem
	rename v8 exit2017
	export excel using "$tables/Table7.xlsx", sheet("employment_table") cell(A1) firstrow(var) sheetreplace
	


*****************************************************************
** IMPACT OF CUT REFORM ON COMPOSITION OF THE FEMALE WORKFORCE **
*****************************************************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (main spells)
	use "$files/estlevel_dataset.dta", clear

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	* Outcomes
	gen y_sh_poachin_tot_fem = poachin_tot_fem/tot_fem
	label var y_sh_poachin_tot_fem 		"Share women poached into firm"	
	gen y_age_tot_fem = age_tot_fem
	label var y_age_tot_fem 			"Mean female age"
	gen y_ten_tot_fem = ten_tot_fem
	label var y_ten_tot_fem 			"Mean female tenure"
	gen y_contracthours_tot_fem = contracthours_tot_fem
	label var y_contracthours_tot_fem 	"Mean female contracted hours"
	gen y_schooling_tot_fem = schooling_tot_fem
	label var y_schooling_tot_fem 		"Mean female years of schooling"

	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}

	//table with baseline mean
	local i = 0
	foreach var of varlist y_sh_poachin_tot_fem y_age_tot_fem y_ten_tot_fem y_contracthours_tot_fem y_schooling_tot_fem {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a`i'
	}			
	#d ;
	estout a1 a2 a3 a4 a5 using "$tables/chars_table.txt", 
	style(tab) mlabels(none) label collabels(none)
	cells(b(star fmt(%9.3f)) se(par)) 
	stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
	drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
	#d cr
	
	import delimited "$tables/chars_table.txt", clear
	rename v1 coeffs
	rename v2 y_sh_poachin_tot_fem
	rename v3 y_age_tot_fem
	rename v4 y_ten_tot_fem
	rename v5 y_contracthours_tot_fem
	rename v6 y_schooling_tot_fem
	export excel using "$tables/TableA14.xlsx", sheet("tableA14") cell(A1) firstrow(var) sheetreplace



********************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (PANEL A) **
** DID FIGS: log wage bill and log wages of new male/fem workers  **
********************************************************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (main spells)
	use "$files/estlevel_dataset.dta", clear

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	*Outcomes
	gen y_ln_wbill_tot = log(wbill_tot)
	label var y_ln_wbill_tot 	"Log wage bill"	
	gen y_lnwage_gwg = lnwage_tot_fem - lnwage_tot_mal
	label var y_lnwage_gwg 	"Gender difference in mean of log earnings"
	gen y_lnwage_oldw_fem = lnwage_oldw_fem
	label var y_lnwage_oldw_fem 	"Mean of log female earnings (established workers)"
	gen y_lnwage_oldw_mal = lnwage_oldw_mal
	label var y_lnwage_oldw_mal 	"Mean of log male earnings (established workers)"
	gen y_lnwage_neww_fem = lnwage_neww_fem
	label var y_lnwage_neww_fem 	"Mean of log female earnings (new workers)"
	gen y_lnwage_neww_mal = lnwage_neww_mal
	label var y_lnwage_neww_mal 	"Mean of log male earnings (new workers)"	

	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}
	
	mdesc y_*
	
	* DID figure for log wages of new female workers and new male workers
	foreach var of varlist y_lnwage_neww_fem y_lnwage_neww_mal {

		reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		local coeff = _b[treatpost]
		local coeff : di %04.3f `coeff'
		local serr = _se[treatpost]
		local serr : di %04.3f `serr'

		reghdfe `var' interj*, absorb(fakeid_estab *FE) cluster(clustergrp)
		estimates store `var'

		#d ;
		coefplot (`var' ,connect(l) lpatt(solid)lcol(sourapple1)
		mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
		text(0.03 4 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
		omitted keep( interj* ) 
		yline(0, lc(sourapple4) lp(solid)) 
		xline(`xl', lc(sourapple4) lp(dash)) 
		vertical ytitle("`: var label `var''", height(5) size(medlarge)) 
		ylabel(-0.04(0.02)0.04, labsize(medium) gmax gmin) 
		xlabel(, labsize(medium))
		plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
		#d cr 
		 
		graph export "$figures/`var'_did.tif", replace

	}
	
	* DID figure for log wage bill
	reghdfe y_ln_wbill_tot treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
	local coeff = _b[treatpost]
	local coeff : di %04.3f `coeff'
	local serr = _se[treatpost]
	local serr : di %04.3f `serr'

	reghdfe y_ln_wbill_tot interj*, absorb(fakeid_estab *FE) cluster(clustergrp)
	estimates store y_ln_wbill_tot

	#d ;
	coefplot (y_ln_wbill_tot ,connect(l) lpatt(solid)lcol(sourapple1)
	mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
	text(0.05 4 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
	omitted keep( interj* ) 
	yline(0, lc(sourapple4) lp(solid)) 
	xline(`xl', lc(sourapple4) lp(dash)) 
	vertical ytitle("`: var label y_ln_wbill_tot'", size(medlarge)) 
	ylabel(-0.06(0.03)0.06, labsize(medium) gmax gmin) 
	xlabel(, labsize(medium))
	plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
	#d cr 
	 
	graph export "$figures/y_ln_wbill_tot_did.tif", replace
	
	//table with baseline mean
	local i = 0
	foreach var of varlist y_ln_wbill_tot y_lnwage_oldw_fem y_lnwage_oldw_mal y_lnwage_neww_fem y_lnwage_neww_mal y_lnwage_gwg {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a`i'
	}			
	#d ;
	estout a1 a2 a3 a4 a5 a6 using "$tables/wages_table.txt", 
	style(tab) mlabels(none) label collabels(none)
	cells(b(star fmt(%9.3f)) se(par)) 
	stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
	drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
	#d cr
	
	import delimited "$tables/wages_table.txt", clear
	rename v1 coeffs
	rename v2 y_ln_wbill_tot
	rename v3 y_lnwage_oldw_fem
	rename v4 y_lnwage_oldw_mal
	rename v5 y_lnwage_neww_fem
	rename v6 y_lnwage_neww_mal
	rename v7 y_lnwage_gwg
	export excel using "$tables/Table7.xlsx", sheet("wages_table") cell(A1) firstrow(var) sheetreplace

	
******************************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (PANEL C: absences) **
******************************************************************************

* data toggles
local singest = 0 //0: no restriction; 1: must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: must employ both men and women at basline
local covered = 1 //0: no restriction; 1: must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: must be a signing establishment
local geoglevel = "microregion_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local balanced = 0 //0: no restriction; 1: restrict to the balanced panel

	* Load establishment sample (main spells)
	//merge in absences info
	use "$files/estlevel_dataset.dta", clear
	merge 1:1 fakeid_estab year using "$files/shdaysleave.dta"
	drop if _merge==2
	drop _merge
	merge 1:1 fakeid_estab year using "$files/shdaysleavefem.dta"
	drop if _merge==2
	drop _merge

	* sample restrictions
	if `singest' {
		keep if bl_singest==1
	}
	if `bothFM' {
		keep if bl_bothFM==1
	}
	if `covered' {
		keep if (bl_covered==1)
	}
	if `signing' {
		keep if (bl_signing==1)
	}
	if `balanced' {
		egen xxx = max(exit), by(fakeid_estab)
		keep if xxx==0
		drop xxx
	}
	
	* time varying fixed effects
	//industry
	gen ind2d_num = floor(ind_mode/1e3)
	//geography
	gen state_mode = floor(municipality_mode/1e4)
	rename municipality_mode municipality
	merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
	drop if _merge==2
	drop _merge
	//fixed effects
	rename municipality municipality_mode
	rename microregion microregion_mode
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}

	* for clustering
	egen clustergrp = group(fakeid_estab)

	* event study var 
	gen post=(year>=2015) if year!=.
	gen treatpost=treat*post
	tab year, gen(yearj)
		
	forvalues i = 1/7 {
		gen interj`i'=treat*yearj`i'
	}

	lab var interj1 "2011"
	lab var interj2 "2012"
	lab var interj3 "2013"
	lab var interj4 "2014"
	lab var interj5 "2015"
	lab var interj6 "2016"
	lab var interj7 "2017"

	gen zero=0
	lab var zero "2014"
	replace interj4 = zero

	*Outcomes
	gen y_shdaysleave_alt = (shdaysleave-shdaysleave_50-shdaysleave_60)*100
	label var y_shdaysleave_alt 	"Workdays lost to leaves (%)"

	* Regressions
	if `no2011' {
		drop if year==2011
		drop interj1
		local xl = 3.5
	}
	else {
		local xl = 4.5
	}
	
	mdesc y_*
	
	//table with baseline mean
	local i = 0
	foreach var of varlist y_shdaysleave_alt {
		local i = `i'+1 
		qui reghdfe `var' treatpost, absorb(fakeid_estab *FE) cluster(clustergrp)
		su `var' if treat == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a`i'
	}			
	#d ;
	estout a1 using "$tables/workdays_table.txt", 
	style(tab) mlabels(none) label collabels(none)
	cells(b(star fmt(%9.3f)) se(par)) 
	stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
	drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
	#d cr
	
	import delimited "$tables/workdays_table.txt", clear
	rename v1 coeffs
	rename v2 y_shdaysleave_alt
	export excel using "$tables/Table7.xlsx", sheet("workdays_table") cell(A1) firstrow(var) sheetreplace



log close
